<?                                      
define ("EVENT_PROCESS_WORKING", 100);                                      
define ("EVENT_PROCESS_COMPLETE", 101);                                      
                                       
define ("EVENT_ARTICLE_LIST_BEGIN", 200);                                        
define ("EVENT_ARTICLE_LIST_ROW_COMPLETE", 201);                                        
define ("EVENT_ARTICLE_SUB_ROW_COMPLETE", 202);                                        
define ("EVENT_ARTICLE_LIST_COMPLETE", 203);                                        


define ("EVENT_NZB_BEFORE_BEGIN", 300);                                      
define ("EVENT_NZB_BEGIN", 301);                                      
define ("EVENT_NZB_PROGESS", 302);                                      
define ("EVENT_NZB_ITEM_DOWNLOADED", 303);                                      
define ("EVENT_NZB_ITEM_DOWNLOADING", 304);                                      
define ("EVENT_NZB_TOO_MANY_ITEMS", 305);                                      
define ("EVENT_NZB_RESPONSE_RECEIVED", 306);                                      
define ("EVENT_NZB_SENDING", 307);                                       
define ("EVENT_NZB_PARTIAL_DOWNLOAD", 308);                                        
define ("EVENT_NZB_ABORT", 309);                                        
define ("EVENT_NZB_WRITING_FILE", 311);                                        
define ("EVENT_NZB_WRITING_SEGMENT", 312);                                     
define ("EVENT_NZB_LARGE_FILE", 313);                                        

define ("HOME_PAGE_BEFORE_BEGIN", 400);                                       

define ("EVENT_ARTICLE_THUMBNAIL_ONLOAD", 500);                                        
define ("EVENT_ARTICLE_THUMBNAIL_ONAPPEND", 501);                                        

define ("EVENT_NZB_COMPLETE", 999);                                      
define ("TEST_PAGE_PROGRESS", 998);                                      


$event_execution_time = time();                                     
$cache_event          = NULL;                                      
$cached_script        = "";                                     
                                   
function htrim($a)                   
{                  
    return htmlentities (trim ( str_replace('"','',$a)));                  
}                  
function qtrim($a)                   
{                  
    return htmlentities (stripslashes($a));                  
}                     
function raise_event ()                                      
{                                      


    global $event_execution_time;                                      
    global $cancel_event;                                      
    global $cache_event;                                      
    global $cached_script;                                      
    global $nntp_server;                                      
    global $href;                                      
    global $qs;                                      
                                          
    if ($cancel_event)                                      
    {                                      
        return;                                      
    }                                      
    $args  = func_get_args();                                      
    $event = $args[0];                                       
    $html  = ""; #"\n<!------------------------------- EVENT $event --------------------------------->\n";                                      
                                           
    if ($event==EVENT_PROCESS_WORKING)                                      
    {                                       
          $html .= "\n<script language='javascript'>                                      
            if (document.all('dupdate'))                                      
            {                                      
                 document.all('dupdate').innerHTML = \"&nbsp;" . trim ($args[1]) . "\";\n                                      
            }        </script>\n";                                      
    }                                      
    else if ($event==EVENT_PROCESS_COMPLETE)                                      
    {                                      
        $event_execution_this = time();                                        
        $event_execution_span = $event_execution_this-$event_execution_time;                                       
          $html .= "\n<script language='javascript'>                                      
            if (document.all('dupdate'))                                      
            {                                      
                 document.all('dupdate').innerHTML = \"&nbsp;" . $event_execution_span . " seconds\";\n                                      
            }        </script>\n";                                      
    }                                      
    else if ($event==TEST_PAGE_PROGRESS)                                      
    {                                      
          $span = time() - $args[3];                                      
          $rate = $args[1] / $span;                                      
          $left = $args[2] - $args[1];                                      
          $ram  = $left / $rate;                                      
          $html .= "\n<script language='javascript'>                                      
            if (document.all('iprog'))                                      
            {                                      
                 document.all('iprog').width = 640 * (" . $args[1] . " / " . $args[2] . ");\n                                      
                 document.all('dstat').innerHTML = '".$span.": ".floor($rate)." msg/sec Time remaining: ".round($ram)."';\n                                      
            }        </script>\n";                                      
                                               
    }                                      
    else if ($event==EVENT_ARTICLE_THUMBNAIL_ONLOAD)                                      
    {                                      
        $id     = $args[1];                                      
        $saveas = $args[2];                                      
        $html  .= script_block("var nne=new anime('ANIM" . $id . "');");                                   
        $folder = realpath ("data/temp/thumbs");                                      
        $dh     = opendir( $folder ) or die ("couldn't open directory");                                       
        while (! ( ( $file = readdir ( $dh ) ) == false ) )                                       
        {                                       
            $real     = realpath($folder);                                      
            $fullpath = "$real/$file";                                       
            if (strpos($file,$saveas)!==false)                                      
            {                                       
                $html .= script_block("nne._add('".$file."');");                                        
            }                                       
        }                                      
        closedir( $dh );                                      
    }                                      
    else if ($event==HOME_PAGE_BEFORE_BEGIN)                                      
    {                                      
          

        $code = "if (window.name!='nzbnmain')                                      
        {                                      
            var k=window.open ('".$href."?".$qs."', 'nzbnmain', 'width=750,height=580,resizable=yes,toolbars=no,menubar=no');                                      
            k.focus();                                        
            window.opener = top;                                      
            window.close();                                        
        } \n ";                                       
          
        if ($_GET["sub"]=="nzb") 
        { 
            $code = "";   
        }                                    
        $preview_temp = $args[1];                                      
        $code .= "/*";                                      
        $code .= implode("\n",$preview_temp);                                      
        $code .= "*/";                                      
        foreach ($preview_temp as $tmp)                                       
        {                                      
            $code .= "if ('" . $tmp . "'!='') articles.push ('" . $tmp . "');";                                      
        }                                       

                                       
        $code .= "var mv=new nntp_picture();";                                      
        $code .= "mv.picture='ipreview';";                                      
        $code .= "mv.container='dpreview';";                                       
        $code .= "mv.server=NNTP;";                                       
        $code .= "mv.width=380;";                                      
        $code .= "mv.height=260;";                                      
        $code .= "mv.lookup=true;";                                       
        $code .= "mv.message='X';";                                           
        $code .= "mv.font=16;";                                            
        $code .= "mv.x=6;";                                             
        $code .= "mv.y=220;";                                            
                                               
        $code .= "mv._onload = function () ";                                           
        $code .= "{ ";                                           
        $code .= "  url = url_from_src(mv.source);";                                           
        $code .= "  txt = url.replace('g=', ''); ";                                           
        $code .= "    HTML = 'Newsgroup: <a style=font-size:7pt href=\"skin.php?vt=vt&'+url+'\">'+txt+'</a>';";                                           
        $code .= "    if (txt.length>0&&d('dpreviewsrc').innerHTML!=HTML)";                                           
        $code .= "    { ";                                           
        $code .= "        d('dpreviewsrc').innerHTML = HTML;";                                           
        $code .= "    } ";                                           
        $code .= "  setTimeout ('home_page_trans()', INTERVAL); ";                                           
        $code .= "} \n ";                                           
                                               
        $code .= "mv._onerror = function () ";                                           
        $code .= "{";                                           
        $code .= "  d('dpreview').style.display = 'block';";                                            
        $code .= "  home_page_trans() ";                                           
        $code .= "}";                                           
                                               
                                                
                                              
                                              
                                              
        $html .= script_block($code);                                         
    }                                      
    else if ($event==EVENT_ARTICLE_LIST_BEGIN)                                      
    {                                        
         $code  = "var mv=new nntp_picture();";                                      
         $code .= "mv.picture='ipreview';";                                      
         $code .= "mv.container='dpreview';";                                      
         $code .= "mv.infopanel='dserver';";                                      
         $code .= "mv.detailpanel='ddetail';";                                       
         $code .= "mv.server=NNTP;";                                      
         $code .= "mv.group=GROUP;";                                      
         $code .= "mv.width=240;";                                      
         $code .= "mv.height=240;";                                      
         $code .= "mv.autosize=false;";                                      
         $code .= "var no=new article_info();";                                      
         $code .= "no.server=NNTP;";                                      
         $code .= "no.group=GROUP;";                                      
         $code .= "no.preview='" . $args[1] . "';";                                      
         $code .= "var nn=new article_list();";                                      
         $code .= "nn.preview='" . $args[1] . "';";                                      
         $html .= script_block($code);                                        
    }                                           
    else if ($event==EVENT_ARTICLE_LIST_ROW_COMPLETE)                                      
    {                                        
         $html .= script_block("nn.add_group('" . $args[1] . "', '" . trim($args[2]) . "', \"" . trim(htmlentities($args[3])) . "\", '" . trim($args[4]) . "', \"" . trim(htmlentities($args[5])) . "\", \"" . trim(htmlentities($args[6])) . "\");");                                         
          $html .= "\n";   
    }                                      
    else if ($event==EVENT_ARTICLE_SUB_ROW_COMPLETE)                                      
    {                                        
         $html .= script_block("nn.add_article('" . $args[1] . "', '" . trim($args[2]) . "', \"" . trim(htmlentities($args[3])) . "\", '" . trim($args[4]) . "', \"" . trim(htmlentities($args[5])) . "\", \"" . trim(htmlentities($args[6])) . "\");");                                         
          $html .= "\n";   
    }                                      
    else if ($event==EVENT_ARTICLE_LIST_COMPLETE)                                      
    {          
        if ($args[2]=="preview")      
        {      
            $html .= script_block($args[1]);      
        }                               
    }                                      
    else if ($event==EVENT_NZB_BEFORE_BEGIN)                                      
    {                                      
        $event_execution_time = exact_millisec();                                       
                                               
                                              
        $html .= '<table> <tr> <td> <img src="images/msdn.jpg"> </td>                                      
               <td nowrap> <table> <tr> <td nowrap id="td_search_options">';                                       
        $html .= "Searching for " . $args[1] . " articles like";                                                   
        $html .= '</td> </tr> <tr> <td nowrap id="td_search_panel"> ';                                        
        $html .= '<input size="50" name=nzb_replace value="' . htmlentities( $args[2] ) . '">';                                                       
        $html .= "<input type='submit' value='Download NZB' disabled id=nzbdl>";                                       
        $html .= "<input type='submit' value='Retry'  name=nzbretry id=nzbretry>";                                      
        $html .= "<input type='submit' value='Force'  name=nzmanual id=nzmanual>";                                         
        $html .= '</td> </tr> </table> </td> <td id="td_status_panel"> </td> </tr> </table>';                                       
        $html .= '<br><div style="width:100%;border-top:solid 1px #339999;background-color:#CCFFCC;padding:3px">';                                       
        $html .= '<table width="100%" cellpadding="1" cellspacing="1">                                      
               <tr> <td> <div id="dpages" style="font:9pt arial">Searching for matching posts. Please wait...';                                       
    }                                      
    else if ($event==EVENT_NZB_PROGESS)                                      
    {                                      
        $args[1] = htrim($args[1]);                 
        #$html .= "\n<" . "script> fx(\"" . $args[1] . "\",\""                                       
        #      . $args[2] . "\",'"                                       
        #      . $args[3] . "','"                                       
        #      . $args[4] . "','"                                       
        #      . $args[5] . "','"                                       
        #      . $args[6] . "');\n </" . "script>\n";                                      
    }                                      
    else if ($event==EVENT_NZB_SENDING)                                      
    {                                       
        $html .= "<" . "script>                                       
                 dm('dmetrics','Sending request (" . $args[1] .")...');                                      
               </" . "script>";                                      
    }                                      
    else if ($event==EVENT_NZB_LARGE_FILE)                                      
    {                                       
        $html .= "<" . "script>                                       
                 window.status = ('" . $args[1] ."');                                      
               </" . "script>";                                      
    }                                       
    else if ($event==EVENT_NZB_RESPONSE_RECEIVED)                                      
    {                                       
        $html .= "<" . "script>                                       
                 dm('dmetrics', 'Response received (" . trim( $args[1] ) .")...');                                      
               </" . "script>";                                        
    }                                       
    else if ($event==EVENT_NZB_ITEM_DOWNLOADED)                                      
    {                                      
        $args[1] = htrim($args[1]);                 
        $html .= "<" . "script>                                       
                D(\"tx_" . $args[1] . "\").bgColor = \"#E0FFE0\";                                      
               </" . "script>";                                      
    }                                       
    else if ($event==EVENT_NZB_ITEM_DOWNLOADING)                                      
    {                                        
        $args[1] = htrim($args[1]);                 
        $html .= "<table id=\"tx_" . $args[1] . "\" bgcolor=\"" . $args[2]. "\" class=\"dh\"><tr>                                       
                   <td><div class=\"hp\"><img height=\"12\" width=\"" . $args[3] . "\" id=\"ix_" . $args[1] . "\" src=\"images/progress.gif\"></div></td>                                      
                   <td width=\"75%\"                                      
                    ><a id=\"dx_" . $args[1] . "\" href=\"javascript:prx(\"" . $args[1] . "\")\">"                                       
                                . $args[4] . "</a></td>                                      
                   <td width=\"100\" id=\"bx_" . $args[1] . "\">" . $args[5] . "</td>                                       
                   <td nowrap>" . $args[6] . "</td>                                       
              </tr></table>";                                       
    }                                       
    else if ($event==EVENT_NZB_TOO_MANY_ITEMS)                                      
    {                                      
        $args[1] = htrim($args[1]);                 
        $html .= "<" . "script>                                       
                D(\"tx_" . $args[1] . "\").bgColor = \"#CCCCFF\";                                      
               </" . "script>";                                      
    }                                           
    else if ($event==EVENT_NZB_WRITING_FILE)                                       
    {                                      
       # $html .= "<" . "script>                                       
       #         Z[\"" . str_replace(" ","_",$args[1]) . "\"]=new Array();                                      
       #        </" . "script>";                                      
    }                                           
    else if ($event==EVENT_NZB_WRITING_SEGMENT)                                      
    {                                      
        #$html .= "<" . "script>                                       
        #        Z[\"" . str_replace(" ","_",$args[1]) . "\"].push (\"" . $args[2] . "\");                                      
        #       </" . "script>";                                      
    }                                           
    else if ($event==EVENT_NZB_BEGIN)                                      
    {                                       
        $event_execution_this = exact_millisec();                                       
        $event_execution_span = $event_execution_this-$event_execution_time;                                       
        $html .= "Response received (" . count ( $args[1] ) . "). Building NZB file...";                                       
        $html .= '</div> </td><td align=right>                                      
           <div id="dmetrics" style="font:9pt arial"></div></td>                                      
           </tr> </table></div><br>';                                       
        $html .= "" . $event_execution_span . " secs<br>";                                      
        if ( count ( $args[1] ) == 0)                                      
        {                                      
            print "No articles  found";                                      
            return false;                                       
        }                                      
                                              
        $html .= "<" . "script language='javascript'>                                       
            var D=document.all;                                      
            var Z = new Array();                                              
            function fx(a,b,c,d,e,f)                                      
            {                                      
                D('dx_'+a).innerHTML = b;                                      
                D('bx_'+a).innerHTML = d;                                      
                D('ix_'+a).width = e;                                       
            }                                      
            function prx(a)                                      
            {                                       
                f = Z[a][0];                                      
                h = Z[a].join (',');                                       
                u='index.php?ret='+h+'&art_group=".get_request("art_group")."&nntp=".$nntp_server."&a=email&message_id='+f;                                      
                window.open (u)                                      
            }                                      
            function dm(a,b)                                      
            {                                      
                D(a).innerHTML = b;                                       
            }                                      
             </" . "script>                                      
             <style>                                      
             body,td {font:8pt tahoma}                                      
             .dh {border-bottom:solid 1px silver;border-top:solid 1px white;                                      
                border-left:solid 1px white;border-right:solid 1px silver;width:100%}                                      
             .hp {width:100px;height:10px;border:solid 1px black}                                      
             </style>";                                       
    }                                      
    else if ($event==EVENT_NZB_COMPLETE)                                      
    {                                      
        $html .= "<" . "script language='javascript'>                                       
            D('nzbdl').disabled = false;                                      
            D('nzb_replace').disabled = false;                                      
            D('dmetrics').innerHTML = '" . $args[1] . " articles, " . number_format($args[2] / 1000, 2, ".", ",") . " Kb.';                                      
            D('dpages').innerHTML = 'Done! Click Download NZB to download your file.';                                      
            </" . "script>";                                      
    }                                      
    else if ($event==EVENT_NZB_ABORT)                                      
    {                                      
        $html .= "<" . "script>                                       
                 dm('td_status_panel', '<img src=\"images/std.gif\" align=left>The search aborted before all reported items could be found.');                                      
               </" . "script>";                                        
    }                                      
    else if ($event==EVENT_NZB_PARTIAL_DOWNLOAD)                                      
    {                                      
        $html .= "<" . "script>                                       
                 alert ('Partial download!');                                     
                 dm('td_status_panel', '<img src=\"images/std.gif\" align=left>Some articles were not found or were only partially downloaded. [".$args[1]."/".$args[2]."] You may want to modify the Search string and click Retry.');                                      
               </" . "script>";                                        
    }                                      
    if ($cache_event)                                  
    {                                  
        $cached_script .= $html;                                  
    }                                  
    else                                  
    {                                  
        echo $html;                                      
    }                                  
    return true;                                      
}                                      


                                       
?>